<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<script type="text/javascript">

$(function(){
		<c:forEach items="${beans}" var="o">
		<c:if test="${o.dataType=='date'}">
			$("#deal${o.fieldId}").datepicker({ dateFormat: 'yy-mm-dd' });
		</c:if>			
		</c:forEach>


	/** 添加 **/	 
	$("#addData").click(function(){
		//tips.text("").removeClass("ui-state-highlight");
		doReset(document.editFm);
		$("#editDIV").dialog("open");
	});

	/** 编辑 **/
	$(".editData").click(function(){
		var id = $(this).attr("id");
		var editURL =myEditUrl + id;
		$.ajax({
			url:editURL,
			type: 'POST',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert('加载数据异常。' + textStatus + "/" + errorThrown);
				$.dialog.alert({title:"保存",content:"加载数据异常"});
			},
			success: function(data){
				fillDIV(data);
			}
		});
		$("#editDIV").dialog("open");
	});

	function doValid(){
		<c:forEach items="${beans}" var="o">
			<c:if test="${!o.nullable}">
			var obj = $("#deal${o.fieldId}");
			if (isNull(obj.val()))
			{
				//alert('${o.title}不能为空！');
				$.dialog.alert({title:"保存",content:"${o.title}不能为空！",objCtrl:"#deal${o.fieldId}"});
				obj.trigger("focus");
				return false;
			}
			</c:if>	
			<c:if test="${o.dataType=='int'}">
			<c:if test="${o.nullable}">
			var obj = $("#deal${o.fieldId}");
			</c:if>	
			if (!isInt(obj.val()))
			{
				//alert('${o.title}须为整数！');
				$.dialog.alert({title:"保存",content:"${o.title}须为整数！",objCtrl:"#deal${o.fieldId}"});
				obj.trigger("focus");
				return false;
			}			
		</c:if>			
		</c:forEach>
		var passwordInput = $("#dealpassword");
		var displayType = $("#dealpassword").css("display");
		if(displayType == "inline-block" || displayType == "block"){
			var password = passwordInput.val();
			if(password){
				var pattern = /[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/g;
				if(!pattern.test(password)||password.length<8){
					$.dialog.alert({title:"编辑用户",content:"密码为8位及以上并且包含字母和数字！",objCtrl:"#dealpassword"});
					return false;
				}
			}
		}
		return true;
	}
	
	$("#editDIV").dialog({
		autoOpen : false,
		bgiframe: true, 
		height : 350,
		width : 450,
		modal : true,
		resizable :false,
       hide: '',
        show:'',
        zIndex:1000,
		buttons : {
			"保 存" : function() {
			  
				var bValid = doValid();
                 
				if(bValid){
					var isAdd = $("#dealid").val() == null || $("#dealid").val() == "0" ;
					var url = isAdd?myAddUrl:mySaveUrl;
					
					$.ajax({
						url:url,
						type: 'POST',
						data: $("#editFm").serialize(),
							
						error: function(XMLHttpRequest, textStatus, errorThrown){
						//alert('保存异常。' + textStatus + "/" + errorThrown);
						 $.dialog.alert({title:"保存",content:"保存异常。"});
						},
						success: function(data){
							if(data.result==1) {
								$("#editDIV").dialog("close");
								if (isAdd) {
									//alert('添加成功.');
									$.dialog.alert({title:"保存",content:"添加成功"});
								}
								else {
									//alert('修改成功.');
									$.dialog.alert({title:"保存",content:"修改成功"});
								}
								query();
							} else {
								//alert("保存失败："+data.errorMessage);
								if (data.errorMessage)
								{
									//alert("保存失败："+data.errorMessage);
									$.dialog.alert({title:"保存",content:"保存失败："+data.errorMessage});
								}else{
									if (data.indexOf("您的输入含有非法字符")>=0)
									{
										//alert("您的输入含有非法字符");
										$.dialog.alert({title:"保存",content:"您的输入含有非法字符"});
									}else
										//alert("保存失败："+data);
										$.dialog.alert({title:"保存",content:"保存失败："+data});
								}
							}
						}
					});  
				}
			},
			"取 消" : function() {
				$(this).dialog("close");			
			}
		},
		close : function() {
			//allFields.val("").removeClass("ui-state-error");
		}
	});

});

</script>